Imports Magic
Public Class ctrTag
    Dim nTag As Core.Tag
    Dim Fields As Core.Xpe
    Dim vControls() As Control
    Sub New(ByVal vArray As Core.Tag, ByVal vField As Core.Xpe)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SuspendLayout()

        For x As Integer = 0 To Me.Controls.Count - 1
            Me.Controls(x).SuspendLayout()
        Next

        Fields = vField
        nTag = vArray

        ReDim vControls(vField.Children.Length - 1)
        For x As Integer = 0 To vField.Children.Length - 1
            Select Case vField.Children(x).Type
                Case Core.Xpe.FieldType.Float1, Core.Xpe.FieldType.Int1, Core.Xpe.FieldType.Short1, Core.Xpe.FieldType.Char, Core.Xpe.FieldType.String
                    Dim nControl As New ctrValue1
                    nControl.SuspendLayout()
                    nControl.lblName.Text = vField.Children(x).Name
                    AddHandler nControl.ValueChanged, AddressOf Value1Changed
                    vControls(x) = nControl
                Case Core.Xpe.FieldType.Prestructure
                    If nTag.Xpe.FourCC = "ustr" Then
                        Dim nControl As New ctrValue1
                        nControl.SuspendLayout()
                        nControl.Unicode = True
                        nControl.lblName.Text = vField.Children(x).Name
                        AddHandler nControl.ValueChanged, AddressOf Value1Changed
                        vControls(x) = nControl
                    ElseIf nTag.Xpe.FourCC = "str#" Then
                        Dim nControl As New ctrValue1
                        nControl.SuspendLayout()
                        nControl.Unicode = False
                        nControl.lblName.Text = vField.Children(x).Name
                        AddHandler nControl.ValueChanged, AddressOf Value1Changed
                        vControls(x) = nControl
                    End If
                Case Core.Xpe.FieldType.Float2, Core.Xpe.FieldType.Int2, Core.Xpe.FieldType.Short2
                    Dim nControl As New ctrValue2
                    nControl.SuspendLayout()
                    nControl.lblName.Text = vField.Children(x).Name
                    AddHandler nControl.ValueChanged, AddressOf Value2Changed
                    vControls(x) = nControl
                Case Core.Xpe.FieldType.Float3, Core.Xpe.FieldType.Int3, Core.Xpe.FieldType.Short3
                    Dim nControl As ctrValue3 = New ctrValue3
                    nControl.SuspendLayout()
                    nControl.lblName.Text = vField.Children(x).Name
                    AddHandler nControl.ValueChanged, AddressOf Value3Changed
                    vControls(x) = nControl

                Case Core.Xpe.FieldType.Block
                    Dim BlockArray As Array
                    If nTag(vField.Children(x).Name).GetLength(0) > 0 Then
                        BlockArray = nTag(vField.Children(x).Name)
                    Else
                        BlockArray = Magic.Core.Xpe.DefaultValue(Core.Xpe.FieldType.Block, 0, vField.Children.Count)
                    End If
                    'If BlockArray.GetLength(0) > 0 Then
                    Dim nControl As New ctrReflexive(BlockArray, vField.Children(x), nTag.Xpe.FourCC)

                    vControls(x) = nControl
                    'End If
                    AddHandler nControl.ValueChanged, AddressOf ReflexiveChanged
                Case Core.Xpe.FieldType.DWordFlags, Core.Xpe.FieldType.WordFlags, Core.Xpe.FieldType.CharFlags
                    If vField.Children(x).Arguments.Length > 0 Then
                        Dim nControl As ctrFlags = New ctrFlags
                        nControl.lblName.Text = vField.Children(x).Name
                        AddHandler nControl.ValueChanged, AddressOf FlagChanged
                        nControl.cList.Items.AddRange(vField.Children(x).Arguments)
                        If vField.Children(x).Arguments.Length > 10 Then
                            nControl.Height = 10 * 15 + 7
                        Else
                            nControl.Height = vField.Children(x).Arguments.Length * 15 + 7
                        End If
                        vControls(x) = nControl
                    End If
                Case Core.Xpe.FieldType.Float4, Core.Xpe.FieldType.Int4, Core.Xpe.FieldType.Short4
                    Dim nControl As ctrValue4 = New ctrValue4
                    nControl.SuspendLayout()
                    nControl.lblName.Text = vField.Children(x).Name
                    AddHandler nControl.ValueChanged, AddressOf Value4Changed
                    vControls(x) = nControl
                Case Core.Xpe.FieldType.Enum
                    Dim nControl As ctrEnum = New ctrEnum
                    nControl.SuspendLayout()
                    nControl.lblName.Text = vField.Children(x).Name
                    nControl.EnumValue.Items.AddRange(vField.Children(x).Arguments)
                    AddHandler nControl.ValueChanged, AddressOf EnumChanged
                    vControls(x) = nControl
                Case Core.Xpe.FieldType.TagReference
                    Dim nControl As New ctrReference
                    nControl.SuspendLayout()
                    nControl.lblName.Text = vField.Children(x).Name
                    AddHandler nControl.ValueChanged, AddressOf ReferenceChanged
                    vControls(x) = nControl
            End Select
            If Not IsNothing(vControls(x)) Then
                Me.Controls.Add(vControls(x))
                ' vControls(x).Dock = DockStyle.Top
                'vControls(x).BringToFront()
                Me.Height += vControls(x).Height + 6
                'If vControls(x).Width > Me.Width Then Me.Width = vControls(x).Width
                vControls(x).Tag = x
            End If
        Next
        For x As Integer = 0 To vControls.Count - 1
            If Not IsNothing(vControls(x)) Then
                If vControls(x).Width >= Me.Width Then
                    Me.Width = vControls(x).Width + 9
                End If
            End If
        Next
        DisplayValues()

        For x As Integer = 0 To Me.Controls.Count - 1
            Me.Controls(x).ResumeLayout()
        Next
        Me.ResumeLayout()

    End Sub

    Private Sub DisplayValues()
        For x As Integer = 0 To Fields.Children.Length - 1
            If Not IsNothing(vControls(x)) Then
                Select Case Fields.Children(x).Type
                    Case Core.Xpe.FieldType.Prestructure
                        If nTag.Xpe.FourCC = "ustr" Or nTag.Xpe.FourCC = "str#" Then
                            Dim nControl As ctrValue1 = vControls(x)
                            nControl.UserDidIt = False
                            If nControl.Unicode = True Then
                                nControl.txtValue.Text = System.Text.UnicodeEncoding.Unicode.GetString(nTag(x)).Trim(Chr(0))
                            Else
                                nControl.txtValue.Text = System.Text.ASCIIEncoding.ASCII.GetString(nTag(x)).Trim(Chr(0))
                            End If
                            nControl.UserDidIt = True
                        End If
                    Case Core.Xpe.FieldType.Float1, Core.Xpe.FieldType.Int1, Core.Xpe.FieldType.Short1, Core.Xpe.FieldType.Char, Core.Xpe.FieldType.String
                        Dim nControl As ctrValue1 = vControls(x)
                        nControl.UserDidIt = False
                        nControl.txtValue.Text = nTag(x)
                        nControl.UserDidIt = True
                    Case Core.Xpe.FieldType.Float2, Core.Xpe.FieldType.Int2, Core.Xpe.FieldType.Short2
                        Dim nControl As ctrValue2 = vControls(x)
                        nControl.UserDidIt = False
                        nControl.txtValue.Text = nTag(x)(0)
                        nControl.txtValue2.Text = nTag(x)(1)
                        nControl.UserDidIt = True
                    Case Core.Xpe.FieldType.Float3, Core.Xpe.FieldType.Int3, Core.Xpe.FieldType.Short3
                        Dim nControl As ctrValue3 = vControls(x)
                        nControl.UserDidIt = False
                        nControl.txtValue.Text = nTag(x)(0)
                        nControl.txtValue2.Text = nTag(x)(1)
                        nControl.txtValue3.Text = nTag(x)(2)
                        nControl.UserDidIt = True
                    Case Core.Xpe.FieldType.Block
                        Dim nControl As ctrReflexive = vControls(x)
                        nControl.UpdateArray(nTag(x))
                    Case Core.Xpe.FieldType.DWordFlags, Core.Xpe.FieldType.WordFlags, Core.Xpe.FieldType.CharFlags
                        Dim nControl As ctrFlags = vControls(x)
                        nControl.bMask.Value = CInt(nTag(x))
                        nControl.SetItems()
                    Case Core.Xpe.FieldType.Float4, Core.Xpe.FieldType.Int4, Core.Xpe.FieldType.Short4
                        Dim nControl As ctrValue4 = vControls(x)
                        nControl.UserDidIt = False
                        nControl.txtValue.Text = nTag(x)(0)
                        nControl.txtValue2.Text = nTag(x)(1)
                        nControl.txtValue3.Text = nTag(x)(2)
                        nControl.txtValue4.Text = nTag(x)(3)
                        nControl.UserDidIt = True
                    Case Core.Xpe.FieldType.Enum
                        Dim nControl As ctrEnum = vControls(x)
                        nControl.UserDidIt = False
                        nControl.EnumValue.SelectedIndex = nTag(x)
                        nControl.UserDidIt = True
                    Case Core.Xpe.FieldType.TagReference
                        Dim nControl As ctrReference = vControls(x)
                        nControl.UserDidIt = False
                        If nTag(x) > -1 Then
                            nControl.txtTag.Text = magicMap.Library(nTag(x)).Name.Replace(appSettings.XboxTagFolder & "\", "")
                        Else
                            nControl.txtTag.Text = ""
                        End If
                        nControl.UserDidIt = True
                End Select
            End If
        Next
    End Sub
    Sub Value1Changed(ByVal sender As ctrValue1)
        Select Case Fields.Children(CInt(sender.Tag)).Type
            Case Core.Xpe.FieldType.Prestructure
                If nTag.Xpe.FourCC = "ustr" Or nTag.Xpe.FourCC = "str#" Then
                    If sender.Unicode = True Then
                        nTag(CInt(sender.Tag)) = System.Text.UnicodeEncoding.Unicode.GetBytes(sender.txtValue.Text & Chr(0))
                    Else
                        nTag(CInt(sender.Tag)) = System.Text.ASCIIEncoding.ASCII.GetBytes(sender.txtValue.Text & Chr(0))
                    End If
                End If
            Case Else
                nTag(CInt(sender.Tag)) = sender.txtValue.Text
        End Select
        nTag.Update()
    End Sub
    Sub Value2Changed(ByVal sender As ctrValue2)
        Dim tmpArray As Array = nTag(CInt(sender.Tag))
        tmpArray(0) = sender.txtValue.Text
        tmpArray(1) = sender.txtValue2.Text
        nTag(CInt(sender.Tag)) = tmpArray
        nTag.Update()
    End Sub
    Sub Value3Changed(ByVal sender As ctrValue3)
        Dim tmpArray As Array = nTag(CInt(sender.Tag))
        tmpArray(0) = sender.txtValue.Text
        tmpArray(1) = sender.txtValue2.Text
        tmpArray(2) = sender.txtValue3.Text
        nTag(CInt(sender.Tag)) = tmpArray
        nTag.Update()
    End Sub
    Sub Value4Changed(ByVal sender As ctrValue4)
        Dim tmpArray As Array = nTag(CInt(sender.Tag))
        tmpArray(0) = sender.txtValue.Text
        tmpArray(1) = sender.txtValue2.Text
        tmpArray(2) = sender.txtValue3.Text
        tmpArray(3) = sender.txtValue4.Text
        nTag(CInt(sender.Tag)) = tmpArray
        nTag.Update()
    End Sub
    Sub EnumChanged(ByVal sender As ctrEnum)
        nTag(CInt(sender.Tag)) = sender.EnumValue.SelectedIndex
        nTag.Update()
    End Sub
    Sub FlagChanged(ByVal sender As ctrFlags)
        nTag(CInt(sender.Tag)) = sender.bMask.Value
        nTag.Update()
    End Sub
    Sub ReferenceChanged(ByVal sender As ctrReference)
        Dim tagDetail(1) As String
        tagDetail(0) = sender.txtTag.Text.Replace(System.IO.Path.GetExtension(sender.txtTag.Text), "")
        tagDetail(1) = cxpe.FromExtension(System.IO.Path.GetExtension(sender.txtTag.Text).Replace(".", "")).FourCC
        nTag(CInt(sender.Tag)) = magicMap.Library.GetDependencyTag(tagDetail(0), tagDetail(1), appSettings.XboxTagFolder, appSettings.CETagFolder)
        nTag.Update()
    End Sub
    Sub ReflexiveChanged(ByVal sender As ctrReflexive)
        nTag(CInt(sender.Tag)) = sender.ValuesArray
        nTag.Update()
    End Sub
End Class
